Skip to content

[Xamarin.Android.Build.Tasks] Scope CoreCLR R8 implements rule to interfaces - #13850

Open
jonathanpeppers wants to merge 3 commits into
mainfrom
jonathanpeppers-r8-typemap-scoped-implements-rule
Open

jonathanpeppers wants to merge 3 commits into
mainfrom
jonathanpeppers-r8-typemap-scoped-implements-rule

Conversation

@jonathanpeppers

@jonathanpeppers jonathanpeppers commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: a69e988685d23538be3911fbc304eb9226f04960

Dimension Assessment Rationale
Change confidence 8.5/10 ↑ After merging main: Mono.Android.NET-Tests Release (CoreCLR, R8, full trim) on a Pixel 7: 1,155 passed, 0 failed.
↑ MAUI app UI check (click, picker, alert, navigation, CollectionView) passed on device.
↑ R8 source reviewed: library interfaces are never pruned; program interfaces stay live through -keep interface K.
↓ Checked with one R8 version only.
↓ CI results for this revision not yet inspected.

On the CoreCLR retained-typemap R8 path (PublishTrimmed=true, $(AndroidLinkTool)=r8), proguard_typemap_coreclr.cfg has this rule:

-keep,allowshrinking class * implements **

The rule exists because managed interface proxy selection (TrimmableTypeMap.TryMatchInterfaces) calls Class.getInterfaces() and R8 cannot see that. But R8's implements also matches superclasses, so ** matches nearly every class. In a dotnet new maui app it blocked optimization of 4,117 of 4,893 live classes (5,745 items).

The proxy lookup only needs the interface edges of classes that implement a bound Java interface (a retained typemap key). The <GenerateTypeMapProguardConfiguration/> task now writes these rules for each key on CoreCLR, in proguard_project_references.cfg:

-if interface K
-keep,allowshrinking,allowobfuscation class * implements K
  • The typemap keys do not say whether a key is a class or an interface. -if interface K applies the rule only to interface keys. Without it, a class key such as android.app.Activity would block optimization of all its subclasses.
  • R8 matches implements through super-interfaces and superclasses, so a class that implements an unbound interface that extends K is still kept.
  • R8 never removes library interfaces (such as android.*) from a class, so the rules only have an effect for interfaces in the app's Java code (AndroidX, Kotlin, and so on).
  • allowobfuscation: the lookup compares interface names, not implementer names. It has no effect today, because this path uses -dontobfuscate.

An alternative was an unconditional rule for interface keys only, which needs an interface list from the key extractor. It was measured and was worse: it also blocks optimization of classes that implement android.* interfaces, for no benefit.

NativeAOT output does not change.

Results

Release .aab, local SDK built from this branch after merging main, net12. "Before" puts back the old rule with an extra ProGuard file on the same SDK. These are the r8.json values that Google Play uses. "Final items" is the number of classes + fields + methods in the output DEX.

App noObfuscation % noOptimization % noShrinking % Final items Items not optimizable Items not shrinkable DEX bytes
dotnet new maui before 99.85 39.34 28.15 37,732 14,844 10,622 3,472,892
dotnet new maui after 99.98 32.72 31.70 33,386 10,924 10,583 3,138,316
dotnet new android before 100.0 85.07 84.33 134 114 113 16,160
dotnet new android after 100.0 84.33 84.33 134 113 113 16,160

noShrinking % goes up only because R8StatsMetadataImpl divides by the number of items in the final output, and R8 now optimizes away about 4,350 more items. The number of items that cannot be shrunk does not change (10,622 → 10,583).

Validation

  • dotnet test Microsoft.Android.Build.Tasks.Tests.csproj --filter FullyQualifiedName~TypeMapProguard -p:_NativeAotLlvmReadObjPath=<NDK llvm-readobj>: 126 passed.
  • Xamarin.Android.Build.Tests.dll, R8Tests: 31 passed. RetainedTypeMapRulesDriveR8 (CoreCLR and NativeAOT): 2 passed. ReleaseCoreClrTrimmableTypeMap_SingleRuntimeIdentifier_PackagesLinkedOrReadyToRunTypeMapAssemblies: passed.
  • Mono.Android.NET-Tests -c Release on a Pixel 7 (CoreCLR, R8, full trimming, 339 -if interface rules): 1,155 passed, 1 skipped (NativeAOT only), 0 failed.
  • dotnet new android and dotnet new maui apps start on a Pixel 7 with no errors in logcat. The MAUI UI check (button click, picker, alert, page navigation, CollectionView scroll) passed.

jonathanpeppers and others added 2 commits November 30, 2026 15:52
The CoreCLR typemap path kept every class that implements anything:

    -keep,allowshrinking class * implements **

R8 matches `implements` also against superclasses, so this rule
restricted almost every live class (MAUI: 5,269 items, 4,134 of 4,910
classes) from optimization.

Managed interface proxy selection only needs the interface edges of
classes that implement a bound Java interface. On CoreCLR,
GenerateTypeMapProguardConfiguration now writes for each retained
typemap key:

    -if interface K
    -keep,allowshrinking,allowobfuscation class * implements K

* `-if interface` applies the rule only when K is an interface, so
  class keys do not pin all their subclasses.
* R8 matches `implements` transitively (super-interfaces and
  superclasses), so implementers of an unbound interface that extends
  K still match.
* R8 never prunes library interfaces (they are always live), so these
  rules have effect only for program interfaces (AndroidX, Kotlin, ...).
* `allowobfuscation`: proxy selection compares interface names, not the
  implementing class names. This has no effect today because the typemap
  path uses -dontobfuscate.

NativeAOT output is unchanged.

`dotnet new maui` (net12, Release .aab, Pixel 7 verified):
obf/opt/shr 0.15/51.52/62.24 -> 0.02/57.06/58.02,
DEX 3,603,728 -> 3,277,708 bytes.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 30, 2026 21:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the change to scope CoreCLR’s interface-preservation rules.

No open issues 🎉

What changed in this PR

This PR narrows CoreCLR’s retained-typemap R8 rules to preserve bound interface relationships without blocking optimization across nearly all Java classes. NativeAOT output remains unchanged.

Changes:

  • Generate conditional implementation-preservation rules for each retained interface key.
  • Remove the broad CoreCLR implements ** rule and wire the scoped behavior through MSBuild.
  • Update test expectations and document the new rules.
File Description
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​TrimmableTypeMapBuildTests.cs Update integration assertions for CoreCLR’s conditional interface rules.
src/​Xamarin.Android.Build.Tasks/​Tests/​Xamarin.Android.Build.Tests/​Tasks/​R8Tests.cs Verify the common configuration no longer contains implementation rules.
src/​Xamarin.Android.Build.Tasks/​Resources/​proguard_typemap_coreclr.cfg Remove the broad rule and point to generated interface rules.
src/​Xamarin.Android.Build.Tasks/​Microsoft.Android.Sdk/​targets/​Microsoft.Android.Sdk.TypeMap.Proguard.targets Pass the CoreCLR-specific option to the rule generator.
src/​Microsoft.Android.Build.Tasks/​Tests/​Microsoft.Android.Build.Tasks.Tests/​TypeMapProguardTargetsTests.cs Update expected CoreCLR rule output.
src/​Microsoft.Android.Build.Tasks/​Tasks/​GenerateTypeMapProguardConfiguration.cs Emit conditional interface-implementation rules when enabled.
Documentation/​guides/​D8andR8.md Explain scoped interface preservation and its effect on optimization.

Review effort: Balanced

@jonathanpeppers

Copy link
Copy Markdown
Member Author

/azp run Xamarin.Android-PR

@azure-pipelines

Copy link
Copy Markdown
No pipelines are associated with this pull request.

@jonathanpeppers

Copy link
Copy Markdown
Member Author

Internal Xamarin.Android-PR build 16171892 failed. I reviewed the failures, and none of them appear to be caused by this PR. They can be ignored:

Failure Why it can be ignored
Task: PoliCheck English / Run PoliCheck Post Analysis Fails on every main build. #13838 fixes it.
Task: Windows > Build & Smoke Test / Ensure no modified/untracked files Untracked .sarif file left by the Guardian tools. Fails on every main build.
Test: AndroidUpdateResourcesTest.CheckNoVersionVectors (CoreCLR) Network error in the log: Failed to download package.
Test: NuGetPackageVersionFinderTests.ShortTaskNameResolvesWithNuGetMetadataInFullMSBuild Fails on every main build.

The public CI (dnceng-public) is offline, so this internal build is the only CI result.

@jonathanpeppers jonathanpeppers added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Dec 1, 2026
@jonathanpeppers

Copy link
Copy Markdown
Member Author

/review

@jonathanpeppers

Copy link
Copy Markdown
Member Author

@dalexsoto review

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13850

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Needs Changes — CI is not green.

Reviewed all seven changed files at eb651de, including the runtime interface-proxy traversal, task inheritance, CoreCLR/NativeAOT gating, and incremental inputs. No substantiated code findings to post inline. The interface guard scopes the optimization restriction without changing NativeAOT rules; the description includes before/after size measurements and device-validation results.

CI: Both returned GitHub check runs passed, but Xamarin.Android-PR reports failure for [build 16171892]((devdiv.visualstudio.com/redacted) The maintainer attributes its failures to existing issues/network errors and reports public CI offline. I could not independently verify those classifications because access to the Azure timeline was denied. No local builds or tests were run in this review.

Generated by Android PR Reviewer for #13850 · copilot · gpt61 · ~57 AIC · ⌖ ~8 AIC · ⊞ 23.1K
Comment /review to run again

@dalexsoto dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed all seven changed files at eb651de, the R8/target wiring, and the exact runtime implementation at this head and current target. The scoped interface rules and removal of the global rule share the same CoreCLR feature gate; NativeAOT and MonoVM behavior is unchanged. Runtime proxy lookup preserves key interface names while allowing intermediate implementers to be renamed. Current-target ProGuard and runtime-remapping changes remain compatible. No blocking issues found. LGTM.

Static review only; I did not run builds or tests.

…pemap-scoped-implements-rule

# Conflicts:
#	Documentation/guides/D8andR8.md
#	src/Microsoft.Android.Build.Tasks/Tests/Microsoft.Android.Build.Tasks.Tests/TypeMapProguardTargetsTests.cs
@jonathanpeppers

Copy link
Copy Markdown
Member Author

/azp run Xamarin.Android-PR

@azure-pipelines

Copy link
Copy Markdown
No pipelines are associated with this pull request.

@dalexsoto dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the current seven-file change and the merge of main, including R8 interface-retention rules, generator/header integration, affected test expectations, and CoreCLR versus MonoVM/NativeAOT gating. No actionable blocking issues found after the separate final pass. Static review only; no builds, tests, generators, or devices were run. Pending CI alone is not a finding.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants